/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.gui.codeview;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;

public class CodeViewUtil {
	
	public static String JAVA_FILE ="java";
	public static String JSP_FILE ="jsp";
	public static String XML_FILE ="xml";

	/**
     * Reads the java source file at the specified string and returns an
     * HTML version stylized for display
     */
    public static String loadSourceCode(String sourceText) {
        CodeStyler cv = new CodeStyler();        
        String styledCode = "<html><body bgcolor=\"#ffffff\"><pre>";
        
        try {
        	
        	if( sourceText == null || sourceText.trim().equals("")){
        		styledCode += cv.syntaxHighlight("没有code生成");
        	} else {
        		Scanner scanner = new Scanner(sourceText);
                while (scanner.hasNextLine()) {
                  String line = scanner.nextLine();
                  // process the line
                  styledCode += cv.syntaxHighlight(line) + " \n ";
                }
        	}
            styledCode += "</pre></body></html>";
            
        } catch (Exception ex) {
            ex.printStackTrace();
            return "Could not load file from: ";
        } 
        return styledCode;
    }
    
    /**
     * Reads the java source file at the specified URL and returns an
     * HTML version stylized for display
     */
    public static String loadSourceCode(URL sourceUrl) {
        InputStreamReader isr = null;
        CodeStyler cv = new CodeStyler();        
        String styledCode = "<html><body bgcolor=\"#ffffff\"><pre>";
        
        try {
            isr = new InputStreamReader(sourceUrl.openStream(), "UTF-8");
            BufferedReader reader = new BufferedReader(isr);
            
            // Read one line at a time, htmlizing using super-spiffy
            // html java code formating utility from www.CoolServlets.com
            String line = reader.readLine();
            while(line != null) {
                styledCode += cv.syntaxHighlight(line) + " \n ";
                line = reader.readLine();
            }
            styledCode += "</pre></body></html>";
            
        } catch (Exception ex) {
            ex.printStackTrace();
            return "Could not load file from: ";
        } 
        finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
        }
        return styledCode;
    }
}
